<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/16
 * Time: 21:00
 */
namespace Pay\Controller;

use Common\Common\Api\Order;
use Common\Common\Api\Ordermember;
use Common\Common\Api\Redis\Channel;
use Common\Common\Api\Redis\Product;

/**
 * Class ManyiController
 * @package Pay\Controller
 * btpay
 */
class BtpayController extends PayController{
    protected $config;
    protected $class;
    public function __construct(){
        parent::__construct();
        $classes = explode("\\",__CLASS__);
        $class = end($classes);
        $this->class = substr($class, 0, stripos($class, "Controller"));
    }

    public function pay($array){
        $this->config = Channel::getInstance()->getById($array['channel']);

        if(!$this->config){
            $this->showmessage('通道错误!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        $product = Product::getInstance()->getById(I("request.pay_bankcode",0,"intval"));

        if(!$product){
            $this->showmessage('未找到支付产品!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        $parameter = array(
            'code' => $this->config['code'], // 通道名称
            'title' => $this->config['title'],
            'exchange' => 1, // 金额比例
            'gateway' => $this->config['gateway'],
            'orderid' => createOrderId(),
            'out_trade_id' => I("request.pay_orderid"),
            'body' => 'pay',
            'channel'=>$array
        );

        $isMobile = isMobile();

        $return = $this->orderadd( $parameter );

        $data = array();
        $data['customerId'] = $this->config['mch_id'];
        $data['orderAmount'] = $return['amount'];
        $data['orderCurrency'] = 'CNY';
        $data['orderNo'] = $return['orderid'];
        $data['pickupUrl'] = $return['callbackurl'];
        $data['receiveUrl'] = $return['notifyurl'];
        $data['signType'] = 'MD5';

        //sign = md5( pickupUrl + receiveUrl + signType + orderNo + orderAmount + orderCurrency + customerId + md5key)
        $str = $data['pickupUrl'].$data['receiveUrl'].$data['signType'].$data['orderNo'].$data['orderAmount'].$data['orderCurrency'].$data['customerId'].$this->config['signkey'];
        $data['sign'] = md5($str);

        po_get($this->config['gateway'],$data);

    }

    //通知
    public function notifyurl(){
        //file_put_contents("a.txt",var_export($_POST,true)."\r\n\r\n");
        //file_put_contents("a.txt",var_export($_GET,true)."\r\n\r\n", FILE_APPEND);
        //file_put_contents("a.txt", file_get_contents('php://input')."\r\n\r\n", FILE_APPEND);
        //file_put_contents("a.txt", date("Y-m-d H:i:s",time())."\r\n\r\n", FILE_APPEND);

        $p = I("post.");

        /*$p = array (
            'orderNo' => '2018122717481035427',
            'orderAmount' => '1.43',
            'orderCurrency' => 'USDT',
            'paymentAmount' => '10.00',
            'transactionId' => 'C2018122717481069582',
            'status' => 'success',
            'signType' => 'MD5',
            'sign' => 'e010384c5ba413813821ad46ec9e8162',
        );*/

        if(!$p || empty($p)){
            exit("wrong");
        }

        //获取订单信息
        $orderid = $p['orderNo'];
        $find = M("Order")->where(array("pay_orderid"=>$orderid))->find();
        if(!$find){
            echo 'order_not_found'; exit;
        }

        //保存回调数据
        M("Order")->where(array("pay_orderid"=>$orderid))->save(array(
            "server_return"=> httpstr($p),
            "pay_orderid_rd"=>$p['transactionId']
        ));

        //获取渠道信息
        $this->config = M("Channel")->where(array('code'=>$this->class))->find();

        $sign = $p['sign'];
        unset($p['sign']);

        //md5( signType + orderNo + orderAmount + orderCurrency + transactionId + status + md5key)
        $str = $p['signType'].$p['orderNo'].$p['orderAmount'].$p['orderCurrency'].$p['transactionId'].$p['status'].$this->config['signkey'];
        $sign2 = md5($str);

        if($sign !== $sign2){
            echo 'sign wrong'; exit;
        }

        if($p['status'] !== 'success'){
            echo 'pay fail'; exit;
        }

        $this->async($find['pay_orderid'],'success');
    }

    public function callBackUrl(){

        echo '处理成功'; exit;
    }
}